/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package guiadvancedjava;

import apple.laf.AquaMenuUI.ChangeHandler;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import java.lang.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author viraksun
 */
public class FrontGUI extends JFrame {

    static final long serialVersionUID = 1L;
    private JPanel panel;
    private Shape clipShape;
    private static final int DEFAULT_WIDTH = 950;
    private static final int DEFAULT_HEIGHT = 600;
    private String[] dayStrings = {"Week Day", "Saturday", "Sunday"};
    private String[] hourStrings = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
    private String[] minStrings = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
        "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
        "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
        "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
        "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
    };
    private String[] halfTStrings = {"am", "pm"};
    
    private JButton startBtn;
    private JButton stopBtn;
    
    private JComboBox dayBox;
    private JComboBox hoursBox;
    private JComboBox minBox;
    private JComboBox halfBox;
    
    private JSlider speedSlider;
    private JSlider zoomSlider;
    

    public FrontGUI() {

        //=================== WINDOWS SETTINGS
        setTitle("GUI interface for Advanced Java project");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);


        /*
         *  THIS SECTION CREATE THE NAVIGATION PANEL 
         * 
         * */

        //NAVIGATION PANEL
        JPanel navigationPanel = new JPanel();
        //navigationPanel.setLayout(new GridLayout(1, 3, 10, 10));        
        navigationPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        navigationPanel.setPreferredSize(new Dimension(200, 100));

        //BUTTONS LOCATED ON THE LEFT OF THE NAVIGATION PANEL
        JPanel leftBtnsPanel = new JPanel();
        leftBtnsPanel.setLayout(new BoxLayout(leftBtnsPanel, BoxLayout.Y_AXIS)); //Component from left to right
        leftBtnsPanel.setPreferredSize(new Dimension(80, 75));


            //SET UP START AND STOP BUTTONS
            startBtn = new JButton("Start");
            startBtn.setPreferredSize(new Dimension(80, 40));
            stopBtn = new JButton("Stop");
            stopBtn.setPreferredSize(new Dimension(80, 40));
            stopBtn.setEnabled(false);

            //ADD BUTTONS TO THE LEFT BUTTONS PANEL
            leftBtnsPanel.add(startBtn);
            leftBtnsPanel.add(stopBtn);

        //--------------------------- 
        JPanel setTimePanel = new JPanel();
        setTimePanel.setLayout(new FlowLayout(FlowLayout.LEFT));

            JLabel setTimeLabel = new JLabel("Set Time:");
            dayBox = new JComboBox(dayStrings);

                hoursBox = new JComboBox(hourStrings);
                minBox = new JComboBox(minStrings);
                halfBox = new JComboBox(halfTStrings);

                setTimePanel.add(setTimeLabel);
                setTimePanel.add(dayBox);
                setTimePanel.add(hoursBox);
                setTimePanel.add(minBox);
                setTimePanel.add(halfBox);


        JPanel displayTimePanel = new JPanel();
        displayTimePanel.setLayout(new FlowLayout(FlowLayout.LEFT));

            JLabel currentTimeLabel = new JLabel("Current Time:");
            JLabel displayTimeLabel = new JLabel(" ");
                displayTimeLabel.setPreferredSize(new Dimension(190, 20));
                displayTimeLabel.setForeground(Color.black);
                displayTimeLabel.setBorder(
                        BorderFactory.createCompoundBorder(
                        BorderFactory.createLineBorder(Color.black),
                        BorderFactory.createEmptyBorder(5, 5, 5, 5)));

                displayTimePanel.add(currentTimeLabel);
                displayTimePanel.add(displayTimeLabel);


        //
        JPanel middlePanel = new JPanel();
        middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.Y_AXIS));

            middlePanel.add(setTimePanel);
            middlePanel.add(displayTimePanel);


        //--------------------------- 
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

            JPanel speedPanel = new JPanel();
            speedPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            speedPanel.setAlignmentX((float) 0.02);

                JLabel speedLabel = new JLabel("SPEED: ");
                speedSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 10, 1);
                speedSlider.setMajorTickSpacing(5);
                speedSlider.setMinorTickSpacing(1);
                speedSlider.setPaintTicks(true);
                speedSlider.setPaintLabels(true);
                speedSlider.setPreferredSize(new Dimension(250, 40));
                speedSlider.setSnapToTicks(true);
                //speedSlider.addChangeListener();

                speedPanel.add(speedLabel);
                speedPanel.add(speedSlider);


            JPanel zoomPanel = new JPanel();
            zoomPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            zoomPanel.setAlignmentX((float) 0.02);

                JLabel zoomLabel = new JLabel("ZOOM: ");
                zoomSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 10, 1);
                zoomSlider.setMajorTickSpacing(5);
                zoomSlider.setMinorTickSpacing(1);
                zoomSlider.setPaintTicks(true);
                zoomSlider.setPaintLabels(true);
                zoomSlider.setPreferredSize(new Dimension(250, 40));

            zoomPanel.add(zoomLabel);
            zoomPanel.add(zoomSlider);

        rightPanel.add(speedPanel);
        rightPanel.add(zoomPanel);


        //--------------------------- 
        //ADD PANELS TO THE NAVIGATION PANEL
        navigationPanel.add(leftBtnsPanel);
        navigationPanel.add(middlePanel);
        navigationPanel.add(rightPanel);

        add(navigationPanel, BorderLayout.NORTH);





        /*
         * 
         *  ACTION LISTENER SECTION
         * 
         * */
        //START BUTTON
        startBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                System.out.println("START Btn pressed !!!");
                stopBtn.setEnabled(true);
                startBtn.setEnabled(false);
            }
        });

        //STOP BUTTON
        stopBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                System.out.println("STOP Btn pressed !!!");
                startBtn.setEnabled(true);
                stopBtn.setEnabled(false);
            }
        });
        
        //DAYS COMBO BOX
        dayBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String day = (String) dayBox.getSelectedItem();
                System.out.println("Day box has changed to : "+dayBox.getSelectedIndex()+" -> "+ day);
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        
        //HOURS COMBO BOX
        hoursBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String hour = (String) hoursBox.getSelectedItem();
                System.out.println("Hours box has changed to : "+hoursBox.getSelectedIndex()+" -> "+ hour);
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        
        //MIN COMBO BOX
        minBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String min = (String) minBox.getSelectedItem();
                System.out.println("Min box changed: "+ minBox.getSelectedIndex()+" -> "+ min );
            }
        });
        
        //HALF COMBO BOX
        halfBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String half = (String) halfBox.getSelectedItem();
                System.out.println("Half changed: "+ halfBox.getSelectedIndex()+" -> "+ half);
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        
        //SPEED SLIDER
        speedSlider.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                int speedRequested = speedSlider.getValue();
                System.out.println("the Speed slider changed: "+ speedRequested);
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        
        //ZOOM SLIDER
        zoomSlider.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                int zoomRequested  = zoomSlider.getValue();
                System.out.println("Zoom slider changed: "+ zoomRequested );
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        
        
        



        
        
        
        

        //CREATE THE PANEL
        panel = new JPanel() {

            static final long serialVersionUID = 2L;

            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;

                //if (clipShape == null) {
                clipShape = makeClipShape(g2);
                //}
                g2.draw(clipShape);


                g2.clip(clipShape);                // draw line pattern

                final int NLINES = 30;
                Point2D p = new Point2D.Double(0, 0);
                for (int i = 0; i < NLINES; i++) {
                    double x = (2 * getWidth() * i) / NLINES;
                    double y = (2 * getHeight() * (NLINES - 1 - i)) / NLINES;
                    Point2D q = new Point2D.Double(x, y);
                    g2.draw(new Line2D.Double(p, q));
                }
            }
        };
        add(panel, BorderLayout.CENTER);
    }

    /**
    Makes the clip shape.
    @param g2 the graphics context
    @return the clip shape
     */
    Shape makeClipShape(Graphics2D g2) {
        FontRenderContext context = g2.getFontRenderContext();
        Font f = new Font("Serif", Font.PLAIN, 100);
        GeneralPath clipShape = new GeneralPath();

        TextLayout layout = new TextLayout("Hello", f, context);
        AffineTransform transform = AffineTransform.getTranslateInstance(0, 100);
        Shape outline = layout.getOutline(transform);
        clipShape.append(outline, false);

        layout = new TextLayout("World", f, context);
        transform = AffineTransform.getTranslateInstance(0, 200);
        outline = layout.getOutline(transform);
        clipShape.append(outline, false);
        return clipShape;
    }
    
   
    
    
    
}
